<template>
  <view class="content">
    <view class="main">
      <image class="logo" src="/static/login-log.png" />
      <view class="foot">
        <button v-if="isDefautLogin && isCheck" class="btn" type="default" @click="onDefaut">一键登录</button>
        <button v-if="isCheck && !isDefautLogin" class="btn" type="default" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">一键登录</button>
        <button v-if="!isCheck" class="btn" type="default" @click="showModal">一键登录</button>
        <view class="agreement">
          <image v-if="!isCheck" src="/static/yhj-nocheck.png" @click="checkAgreement" />
          <image v-if="isCheck" src="/static/yhj-check.png" @click="checkAgreement" />
          <text @click="checkAgreement">我已阅读并同意</text>
          <text class="info" @click="goAgreement">《利思泰用户服务协议》</text>
        </view>
      </view>
    </view>
    <uni-popup ref="wxpopup">
      <view class="modal">
        <view class="title">是否已阅读并同意《利思泰用户服务协议》？</view>
        <view class="btns">
          <button class="btn cancel" type="default" @click="cancelAgreement">取消</button>
          <button class="btn confirm" v-if="!isDefautLogin" type="primary" @click="confirmAgreement" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">确认</button>
          <button class="btn confirm" v-if="isDefautLogin" type="primary" @click="onDefaut">确认</button>
        </view>
      </view>
    </uni-popup>
  </view>
</template>
<script setup>
import { ref, reactive, watch, getCurrentInstance, onMounted } from "vue";
import { onLoad, onShow } from "@dcloudio/uni-app";
import {userLoginByCode,weixinMiniAppLogin} from '@/service/user'
import {login} from '@/untils/common'
const isCheck = ref(false)
const wxpopup = ref();
const showModal = ()=>{
  wxpopup.value.open();
}
const cancelAgreement = ()=>{
  wxpopup.value.close();
}
const getPhoneNumber = async (e) => {
  if (e.detail.errMsg !== 'getPhoneNumber:ok') {
      uni.showToast({title:'快捷登录失败',icon:'none'});
      return;
    }
    console.log(isCheck.value,'isCheck.value');
    isCheck.value = true;
    cancelAgreement();
    // if(!isCheck.value){
    //   uni.showModal({
    //     content: '是否已阅读并同意《利思泰平台相关协议》？',
    //     success: function (res) {
    //       if (res.confirm) {
    //         isCheck.value = true;
    //         mobileLogin(e.detail);
    //       } else if (res.cancel) {
    //         console.log('用户点击取消');
    //       }
    //     }
    //   });
    //   return;
    // }
    await mobileLogin(e.detail);
  };
  // 微信小程序手机号授权登陆
const mobileLogin = async (e) => {
  return new Promise(async (resolve, reject) => {
    if (e.errMsg !== 'getPhoneNumber:ok') {
      return resolve(false);
    }

    // 1. 获得微信 code
    const codeResult = await uni.login();
    if (codeResult.errMsg !== 'login:ok') {
      return resolve(false);
    }

    // 2. 一键登录
    const loginResult = await weixinMiniAppLogin({phoneCode:e.code, loginCode:codeResult.code, state:'default'});
    console.log(loginResult,'loginResult');
      uni.setStorageSync('openid', loginResult.openid);
      uni.setStorageSync('accessToken',loginResult.accessToken)
      // uni.switchTab({
      //   url: '/pages/index/index'
      // })
      uni.$emit('phoneLogin');
      uni.$emit('login');
      uni.navigateBack();
      return resolve(true);

    // TODO 芋艿：shareInfo: uni.getStorageSync('shareLog') || {},
  });
};
const onDefaut = ()=>{
  login().then(()=>{
    uni.navigateBack();
    })
}
const checkAgreement = ()=>{
  isCheck.value = !isCheck.value
}
const goAgreement = () => {
  uni.navigateTo({
    url: '/pages/login/agreement'
  });
};
const isDefautLogin = ref(true)
onLoad(()=>{
  login(false).then(()=>{
    isDefautLogin.value = true;
    }).catch(()=>{
      isDefautLogin.value = false;
    })
})
</script>
<style lang='less' scoped>
.content {
  .main {
    position: relative;
    background-color: #fff;
    height: 100vh;
    width: 100%;
    .logo{
      position: absolute;
      top: 30%;
      width: 342rpx;
      height: 112rpx;
      left: 50%;
      transform: translateX(-50%);
    }
    .foot{
      position: absolute;
      bottom: 10%;
      padding: 0 48rpx;
      box-sizing: border-box;
      left: 50%;
      transform: translateX(-50%);
      width: 100%;
      .btn{
        width: 100%;
        height: 100rpx;
        background: linear-gradient(315deg, #00b952 0%, #00e565 100%);
        border-radius: 60rpx;
        line-height: 100rpx;
        text-align: center;
        font-size: 32rpx;
        font-weight: 600;
        color: #ffffff;
        margin-bottom: 24rpx;
      }
      .agreement{
        font-size: 28rpx;
        color: #666666;
        display: flex;
        align-items: center;
        justify-content: center;
        .info{
          color: #009A44;
        }
        image{
          width: 36rpx;
          height: 36rpx;
          margin-right: 24rpx;
        }
      }
    }

  }
  .modal{
    background-color: #fff;
    padding: 30rpx;
    border-radius: 10rpx;
    width: 70%;
    margin: 0 auto;
    .title{
        margin-bottom: 100rpx;
        text-align: center;
        font-weight: 800;
      }
    .btns{
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 24rpx;
     
      .btn{
        width: 40%;

      }
    }
  }
}
</style>